package com.xxx.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xxx.config.Request;
import com.xxx.pojo.Product;
import com.xxx.pojo.ProductDetails;
import com.xxx.pojo.deleted.ClientDeleted;
import com.xxx.pojo.deleted.ProductDeleted;
import com.xxx.pojo.vo.ProductVo;
import com.xxx.service.ClientDeletedService;
import com.xxx.service.ProductDeletedService;
import com.xxx.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author George
 * @since 2023-07-04
 */
@Api(tags = "商品回收")
@CrossOrigin(origins = {"*", "null"})
@RestController
@RequestMapping("/productDeleted")
public class ProductDeletedController {

    @Resource
    private ProductDeletedService productDeletedService;

    /**
     * 查询所有已被下架的商品
     *
     * @return 返回操作结果
     */
    @ApiOperation(value = "根据编号、昵称查询已被禁用的商品", notes = "根据编号、昵称查询已被禁用的商品")
    @PostMapping("/disabledProductById")
    public Request getDisabledProductById(
            @RequestParam("searchValue") String searchValue,
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize) {

        Page<ProductDeleted> mapPage = new Page<>(pageNum, pageSize, true);


        // 去除查询条件中的空格
        String finalSearchValue = StringUtils.isNotBlank(searchValue) ? searchValue.trim() : null;

        MPJLambdaWrapper<ProductDeleted> lambdaWrapper = new MPJLambdaWrapper<ProductDeleted>()
                .selectAll(ProductDeleted.class)
                .selectAll(ProductDetails.class)
                .leftJoin(ProductDetails.class, ProductDetails::getProductId, Product::getProductId);

        if (StringUtils.isNotBlank(finalSearchValue)) {
            lambdaWrapper.and(wrapper -> wrapper
//                    根据商品名称查询
                    .like(Product::getProductName, finalSearchValue)
                    .or()
//                    根据商品品牌查询
                    .like(Product::getProductBrand, finalSearchValue))
                    .or()
//                    根据产品类型查询
                    .like(Product::getProductTypeId, finalSearchValue);
        }

        productDeletedService.selectJoinListPage(mapPage,ProductDeleted.class,lambdaWrapper);
        List<ProductDeleted> records = mapPage.getRecords();
        long total = mapPage.getTotal();

        return records.size() > 0 ? Request.suc(total,records) : Request.error("查询产品失败");
    }



    /**
     * 批量上架商品
     *
     * @param productId 产品编号
     * @return 返回操作结果
     */
    @ApiOperation(value = "批量上架商品", notes = "根据编号批量上架商品")
    @DeleteMapping("/restorePermissionsBatch")
    public Request restorePermissionsBatch(@RequestParam("productId") List<Long> productId) {
        boolean restored = productDeletedService.removeByIds(productId);
        return restored ? Request.suc() : Request.error("根据工号批量恢复用户权限失败");
    }



}

